﻿package com.android.SchoolManagerSystem;

import java.util.ArrayList;
import java.util.List;

import com.android.SchoolManagerSystem.data.Transcript;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CommonResList {
	private static final String TAG = "OnlineCurriculaPage";

	private final Context mContext;
	private View mMainPanel;

	private ListView mListView = null;
	private ListViewAdapter mMainAdapter;
	private final LayoutInflater mInflater;

	protected List<commoncotent> mCommonContentlist;

	public class commoncotent {
		public int iindex = 0;
		public String strContentTitle = null;
		public String strContent = null;
		public boolean bcanedit = false;
	}

	public CommonResList(Context context, View mainPanel) {
		mContext = context;
		mMainPanel = mainPanel;
		mCommonContentlist = new ArrayList<commoncotent>();
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mListView = (ListView) mMainPanel
				.findViewById(R.id.commonrelist_contentmenu);
	}

	public void resetCommoncontentList() {

		mMainAdapter = new ListViewAdapter(mContext);
		mListView.setAdapter(mMainAdapter);
		mListView.setOnItemClickListener(new listViewClicker());
	}

	public void clear() {
		mCommonContentlist.clear();
	}

	public void addcontent(int iindex, String strtitle, String strContent, boolean bedit) {

		commoncotent tmpcontent = new commoncotent();
		tmpcontent.iindex = iindex;
		tmpcontent.strContentTitle = strtitle;
		tmpcontent.strContent = strContent;
		tmpcontent.bcanedit = bedit;
		mCommonContentlist.add(tmpcontent);
	}

	private class listViewClicker implements OnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Log.i(TAG,
					"listViewClicker position : " + Integer.toString(position));

		}
	}

	private View inflateIfNeed(View view, int resource, ViewGroup root,
			boolean attachToRoot) {
		if (view != null) {
			return view;
		}

		return mInflater.inflate(resource, root, attachToRoot);
	}

	private class ListViewAdapter extends BaseAdapter implements
			OnItemClickListener {
		public ListViewAdapter(Context context) {
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			commoncotent strCurInfo = mCommonContentlist.get(position);

			Log.i(TAG, strCurInfo.strContentTitle+"判断结果:" + strCurInfo.bcanedit + "位置："

			+ position);
			
			if (strCurInfo.bcanedit) {
				convertView = inflateIfNeed(convertView,
						R.layout.commonreslistitemedit, parent, false);
			} else {
				convertView = inflateIfNeed(convertView,
						R.layout.commonreslistitem, parent, false);
			}

			//commoncotent strCurInfo = mCommonContentlist.get(position);

			if (strCurInfo.strContentTitle.length() != 0) {
				TextView view = (TextView) convertView
						.findViewById(R.id.commonresitemtitle);
				view.setText(strCurInfo.strContentTitle);
			}
			if (strCurInfo.strContent.length() != 0) {
				TextView viewcontent = (TextView) convertView
						.findViewById(R.id.commonresitemcontent);
				viewcontent.setText(strCurInfo.strContent);
			}

			return convertView;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mCommonContentlist.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub

		}
	}

}
